package com.yazi.mxz.common;

import java.util.List;

import com.myframework.smhj.common.tool.WebContextFactoryUtil;

import com.yazi.mxz.common.entity.beans.Setting;
import com.yazi.mxz.common.entity.enums.SettingId;
import com.yazi.mxz.common.service.SettingService;

public class ComSetting {
	private static List<Setting> setttings;

	public static List<Setting> getSettings() {
		if (null == setttings) {
			reset();
		}
		return setttings;
	}

	public static List<Setting> reset() {
		SettingService settingService = (SettingService) WebContextFactoryUtil
				.getBean("settingService");
		return setttings = settingService.listAll();
	}

	public static Setting getSetting(SettingId index) {
		return getSettings().get(index.ordinal());
	}

	public static String getValue(SettingId index) {
		return getSettings().get(index.ordinal()).getValue();
	}

	public static Integer getInteger(SettingId index) {
		return getValue(index, Integer.class);
	}

	public static Long getLong(SettingId index) {
		return getValue(index, Long.class);
	}

	public static Float getFloat(SettingId index) {
		return getValue(index, Float.class);
	}

	public static Double getDouble(SettingId index) {
		return getValue(index, Double.class);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getValue(SettingId index, Class<T> clazz) {
		String value = getSettings().get(index.ordinal()).getValue();
		if (null == value) {
			return null;
		}
		if (clazz == Integer.class) {
			return (T) new Integer(value);
		} else if (clazz == Long.class) {
			return (T) new Long(value);
		} else if (clazz == Float.class) {
			return (T) new Float(value);
		} else if (clazz == Double.class) {
			return (T) new Double(value);
		} else {
			return (T) value;
		}
	}

}
